#include "GdalLoader.h"
#include "gdal_priv.h"
#include "gdalwarper.h"
#include "ogr_spatialref.h"

GdalLoader::GdalLoader(){
	data = NULL;
}

GdalLoader::~GdalLoader(){
	if (data != NULL) close(data);
}


void GdalLoader::registerGeotiffDriver(){
	GDALRegister_GTiff();
}

GDALDataset* GdalLoader::openFile(const char* path){
	GDALDatasetH handle = GDALOpen(path, GA_ReadOnly);
	if (handle == NULL) return NULL;
	GDALDataset* rv = (GDALDataset*) handle;
	rv->GetGeoTransform(padfTransform);
	width = rv->GetRasterXSize();
	height = rv->GetRasterYSize();
	return rv;
}

void GdalLoader::close(GDALDataset* data){
	GDALClose((GDALDatasetH) data);
}

void GdalLoader::pixelToGeo(double pixelx, double pixely, double* geox, double* geoy){
	*geox = padfTransform[0] + pixelx*padfTransform[1] + pixely*padfTransform[2];
	*geoy = padfTransform[3] + pixelx*padfTransform[4] + pixely*padfTransform[5];
}

void GdalLoader::geoToPixel(double geox, double geoy, double* pixelx, double* pixely){
	*pixelx = (padfTransform[0] - geox) / padfTransform[1] * -1;
	*pixely = (padfTransform[3] - geoy) / padfTransform[5] * -1;
}

int progressCallback(double dfComplete, const char *pszMessage, void *pProgressArg){
	return true;
}

int GdalLoader::getHeight(){
	return height;
}

int GdalLoader::getWidth(){
	return width;
}